<?php

namespace App\Http\Controllers\Mini;
use App\Http\Controllers\Controller;

use Illuminate\Console\Command;
use App\Models\Configs;
use App\Models\Demands;
use App\Models\Users;
use App\Models\rewardOrder;
use App\Models\Test;
use App\Models\BalanceLogs;
use App\Http\Requests\Demand;
use App\Models\Mails;
use Log;
class InspireController extends Controller{
	
    public function index(){//每十分钟执行一次
    	//1、查询任务是否结束
    	$data['status'] = 4;
    	//$map['end_at'] = ['<=',date('Y-m-d H:i:s',time())];
    	Demands::whereDate('end_at','<=',date('Y-m-d H:i:s',time()))->update($data);
    	//任务超时
    	
    	rewardOrder::where("status",1)->whereDate('end_time','<=',date('Y-m-d H:i:s',time()))->update(array("status"=>5));
    	//自动审核
    	$map2=[
    			//'status_end_time' => ['>=',time()],
    			'status' => 2,
    			'check_status'=>0
    	];
    	 
    	$lists = rewardOrder::where($map2)->where('staus_end_time','<=',time())->get();
    	foreach ($lists as $key=>$val){
    		$reward = Demands::where("id",$val['reward_id'])->first();
    		//审核时，已完成数达到任务总数，直接视为失败
    		if($reward['ownnumber']>=$reward['number']){
    			$order['status'] = 4;
    			$order['check_status'] = 2;
    			$info = rewardOrder::findOrFail($val['id']);
    			$info->update($order);
    			$content = '您接受的任务"' .$reward['title'] .'"审核失败，其它赏金猎人先于您一步完成，请下次加油';
    		}else{
    			$order['status'] = 3;
    			$order['check_status'] = 1;
    			$info = rewardOrder::findOrFail($val['id']);
    			$info->update($order);
    			//新增余额
    			Users::where('id',$val['user_id'])->increment('balance',$val['get_money']);
    			//成功数+1
    			Demands::where('id',$val['reward_id'])->increment('ownnumber');
    			$content = '您接受的任务"' .$reward['title'] .'"审核通过，赏金增加'.$val['get_money'];
    	
    			$user_info = Users::where('id',$val['user_id'])->where('status','<>',-1)->first(['true_name','nickname','inviter_id']);
    			$balance = [
    					'user_id' => $val['user_id'],
    					'user_name' => $user_info['true_name']?$user_info['true_name']:$user_info['nickname'],
    					'money' => $val['get_money'],
    					'balance' => '+'.$val['get_money'],
    	
    					'desc' => '任务收益',
    					'type' => 1,
    			];
    			$balance_info = BalanceLogs::create($balance);
    			
    			//悬赏这扣除余额
    			Users::where('id',$reward['user_id'])->decrement('balance',$reward['price']);
    			$balance1 = [
    					'user_id' => $reward['user_id'],
    					'user_name' => Users::where('id',$reward['user_id'])->value('nickname'),
    					'money' => $reward['price'],
    					'balance' => '-'.$reward['price'],
    			
    					'desc' => '支付赏金',
    					'type' => 1,
    			];
    			$balance_info = BalanceLogs::create($balance1);
    			
    			//查询是否有团长，有的话团长金额增加
    			if($user_info['inviter_id']>0){
    				//有上级，查看是否有返佣资格
    				$my_numbers = Users::where(array("inviter_id"=>$user_info['inviter_id'],"type"=>2,"status"=>2))->count("id");
    				if($my_numbers>=10){
    					$base = Configs::getConfig('base');
    					Users::where('id',$user_info['inviter_id'])->increment('balance',$val['get_money']*$base['captain_rate']/100);
    					$msg = '您的团员'.$user_info['nickname'].'完成任务"' .$reward['title'] .'"获得佣金'.$val['get_money']*$base['captain_rate']/100;
    					$template1 = [
    							'title' => '佣金提成通告',
    							'content' => $msg,
    							'user_id' => $user_info['inviter_id'],
    							'status' => 1,
    							'type' => 1
    					];
    					//站内信
    					Mails::create($template1);
    					
    					$inviter = Users::where('id',$user_info['inviter_id'])->where('status','<>',-1)->first(['true_name','nickname','inviter_id']);
    					$balance = [
    							'user_id' => $user_info['inviter_id'],
    							'user_name' => $inviter['true_name']?$inviter['true_name']:$inviter['nickname'],
    							'money' => $val['get_money']*$base['captain_rate']/100,
    							'balance' => '+'.$reward['price']*$base['captain_rate']/100,
    								
    							'desc' => '团员收益提成',
    							'type' => 1,
    					];
    					$balance_info = BalanceLogs::create($balance);
    					
    					
    				}
    			}
    		}
    		$template = [
    				'title' => '任务通告',
    				'content' => $content,
    				'user_id' => $val['user_id'],
    				'status' => 1,
    				'type' => 1
    		];
    		//站内信
    		Mails::create($template);
    	}
       
    	
    }
}
